﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using WeChat.Common;

namespace WeChat.UserInfo
{
    public partial class RechargeRecords : System.Web.UI.Page
    {
        GetUserInfoBLL gb = new GetUserInfoBLL();
        //public string openid;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //认证后得到code值
                if (Request.QueryString["code"] != null || Request.QueryString["code"] != "")
                {
                    string code = Request.QueryString["code"];
                    //ApplicationLog.Write("充值记录code:" + code);
                    if (Session["openid"] == null || Session["openid"].ToString() == "")
                    {
                        Session["openid"] = OAuthHelper.getOpenid(code);
                    }
                    string openid = Session["openid"].ToString();
                    //ApplicationLog.Write("获取到的openid:" + openid);
                    if (!new BindIdBLL().verifyId(openid))//验证是否绑定账号
                    {
                        Response.Redirect("/WeChat/Binding.aspx?openid=" + openid);
                    }
                    else//默认查询一周内充值记录
                    {
                        GetUserInfoBLL gb = new GetUserInfoBLL();
                        DataTable dt = gb.getUserInfo(openid).Tables[0];
                        if ("管理卡".Equals(dt.Rows[0]["ryxm"].ToString()) || dt.Rows[0]["rfkh"].ToString() == "000001")
                        {
                            Response.Redirect("SYSCount.aspx?openid=" + openid);
                        }
                        else
                        {
                            string startTime = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd 00:00:00");
                            string endTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                            //dateslive.Text = "7天内充值记录";
                            dataBind(openid, startTime, endTime);
                        }
                    }
                }
            }
        }

        //days事件，查询记录
        protected void days(object sender, EventArgs e)
        {
            HtmlAnchor htmla = (HtmlAnchor)sender;
            string id = htmla.ID;
            string startTime = "";
            string endTime = "";
            string openid = Session["openid"].ToString();
            switch (id)
            {
                case "A1"://当天时间
                    startTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
                    endTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    break;
                case "A2"://七天内时间
                    startTime = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd 00:00:00");
                    endTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                    break;
                case "A3"://30天内时间
                    startTime = DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd 00:00:00");
                    endTime = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");
                    break;
                default://全部记录
                    startTime = DateTime.Now.AddYears(-1).ToString("yyyy-MM-dd 00:00:00");
                    endTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    break;
            }
            //dateslive.Text = htmla.InnerText;
            dataBind(openid,startTime,endTime);
        }

        public void dataBind(string openids,string startTime,string endTime)
        {
            //ApplicationLog.Write("查看充值记录：" + openids + "," + startTime + "," + endTime);
            //loadingToast.Attributes.Add("style", "display:block");
            DataSet ds = gb.getRechargeInfo(openids, startTime, endTime);
            cdcatalog.DataSource = ds;
            cdcatalog.DataBind();
            //loadingToast.Attributes.CssStyle["display"] = "none";

            if (ds.Tables[0].Rows.Count > 0)
            {
                tb1.Visible = true;

                adds.Text = gb.getAddCount(openids, startTime, endTime) == "" ? "0.00" : Double.Parse(gb.getAddCount(openids, startTime, endTime)).ToString("f2");
                dels.Text = gb.getDelCount(openids, startTime, endTime) == "" ? "0.00" : Double.Parse(gb.getDelCount(openids, startTime, endTime)).ToString("f2");
            }
            else
            {
                tb1.Visible = false;
                //dateslive.Text = "无充值记录";
                //dateslive.Visible = true;
            }
        }
    }
}
